package com.banshuibao.bsb_android.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.MoreCoachAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.MoreCoachModel;
import com.banshuibao.bsb_android.model.NewSearchResultModel;
import com.banshuibao.bsb_android.model.RequestMoreModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;
import com.banshuibao.bsb_android.util.IntentManager;

import java.util.List;

/**
 * Created by wangpan on 2018/3/26.
 */

@SuppressLint("ValidFragment")
public class CoachMoreFragment extends BaseFragment implements PullToRefreshListView.OnRefreshListener2, AdapterView.OnItemClickListener {

    private PullToRefreshListView pullToRefreshListView;
    private ListView listView;
    private String key,title,tag;
    private MoreCoachAdapter adapter;
    private int startId,endId;
    private boolean isUp;

    public CoachMoreFragment(String key, String title) {
        this.key = key;
        this.title = title;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.search_listview_layout, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        pullToRefreshListView = getView().findViewById(R.id.ptrlv);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.loadBar();
        pullToRefreshListView.setOnRefreshListener(this);
        listView = pullToRefreshListView.getRefreshableView();
        listView.setDivider(null);
        adapter = new MoreCoachAdapter(getActivity());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        getData();
    }

    public void getData() {
        isUp = false;
        startId = 1;
        endId = startId + 9;
        if (adapter != null) {
            int count = adapter.getCount();
            if (count > 10) {
                endId += count - 10;
            }
        }
        requestNet();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        getData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        isUp = true;
        endId = startId+9;
        requestNet();
    }

    public void requestNet() {
        RequestMoreModel requestMoreModel = new RequestMoreModel();
        requestMoreModel.setSinceId(startId);
        requestMoreModel.setMaxId(endId);
        requestMoreModel.setType("policy");
        requestMoreModel.setContent(key);
        HttpUtil.doPost(Constants.Url.SEARCH_MORE, requestMoreModel, new HttpResponse(getActivity(), MoreCoachModel.class) {
            @Override
            public void onResult(Object obj) {
                //  String json = (String) obj;
                MoreCoachModel model = (MoreCoachModel) obj;
                List<NewSearchResultModel.DataBean.PolicysBean> policysBeans = model.getData();
                int rows = model.getRows();
                startId += rows;
                if(isUp){
                    adapter.upData(policysBeans);
                }else{
                    adapter.downData(policysBeans);
                }

            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullToRefreshListView.onRefreshComplete();
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(adapter!=null){
            NewSearchResultModel.DataBean.PolicysBean policysBean = adapter.getItem(position - 1);
            IntentManager.goCoachPolicyDetailActivity(getActivity(),policysBean.getUuid(),policysBean.getTitle(),policysBean.getCreateDate());
        }
    }
}
